using System;
using System.Collections.Generic;
using System.Text;

using DeviceManager.Device;
using DeviceManager.Common;
using LoggingClient;
using LittleATM.Interop.KXJournalPrinter;

namespace DeviceManager.Kal.KXDevice
{
    [Trace]
    public class KXJournalPrinterImpl : KXDeviceImpl, IJournalPrinter
    {
        #region Journal Printer Events
        public event DeviceEvent PrintComplete = delegate { };
        public event DeviceEvent LinePrinted = delegate { };

        #endregion

        #region Common Override Code
        KXJournalPrinter journalPrinter = null;

        public override bool Init()
        {
            try
            {
                journalPrinter = new KXJournalPrinter();
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex.ToString());

                return false;
            }

            return true;
        }

        public override bool OpenConnection()
        {
            short ret = journalPrinter.OpenConnection();

            DMLogger.Instance.Info(string.Format("KXJournalPrinterImpl.OpenConnection() = {0}", ret));

            return true;
        }

        public override bool CloseConnection()
        {
            short ret = journalPrinter.CloseConnection();

            DMLogger.Instance.Info(string.Format("KXJournalPrinterImpl.CloseConnection() = {0}", ret));

            return true;
        }

        public override bool SubscribeEvents()
        {
            try
            {
                journalPrinter.ConnectionOpened += OnConnectionOpened;
                journalPrinter.ConnectionClosed += OnConnectionClosed;
                journalPrinter.DeviceError += OnDeviceError;
                journalPrinter.FatalError += OnDeviceError;
                journalPrinter.StatusChanged += OnStatusChanged;
                journalPrinter.Timeout += OnTimeout;
                journalPrinter.Locked += OnLocked;
                journalPrinter.LockTimeout += OnLockTimeout;
                journalPrinter.Unlocked += OnUnlocked;
                journalPrinter.EnabledEventsDeferred += OnEnabledEventsDeferred;

                journalPrinter.PrintComplete += OnPrintComplete;
                journalPrinter.LinePrinted += OnLinePrinted;

                return true;
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex);
                return false;
            }
        }

        public override bool UnSubscribleEvents()
        {
            try
            {
                journalPrinter.ConnectionOpened -= OnConnectionOpened;
                journalPrinter.ConnectionClosed -= OnConnectionClosed;
                journalPrinter.DeviceError -= OnDeviceError;
                journalPrinter.FatalError -= OnDeviceError;
                journalPrinter.StatusChanged -= OnStatusChanged;
                journalPrinter.Timeout -= OnTimeout;
                journalPrinter.Locked -= OnLocked;
                journalPrinter.LockTimeout -= OnLockTimeout;
                journalPrinter.Unlocked -= OnUnlocked;
                journalPrinter.EnabledEventsDeferred -= OnEnabledEventsDeferred;

                journalPrinter.PrintComplete -= OnPrintComplete;
                journalPrinter.LinePrinted -= OnLinePrinted;

                return true;
            }
            catch (Exception ex)
            {
                DMLogger.Instance.Error(ex);
                return false;
            }
        }
        #endregion

        #region Events
        public void OnPrintComplete()
        {
            RaiseProtectedEvent(PrintComplete);
        }

        public void OnLinePrinted()
        {
            RaiseProtectedEvent(LinePrinted);
        }
        #endregion

        #region IJournalPrinter Interface Impl
        public bool PrintToPaperJournal(string FormName, string FieldValues)
        {
            short ret = journalPrinter.Print(FormName, FieldValues);

            return true;
        }

        public string GetPaperStatus()
        {
            return journalPrinter.StPaperStatus;
        }

        public string GetMediaStatus()
        {
            return journalPrinter.StMediaStatus;
        }
        #endregion
    }
}
